package com.asiainfo.lop;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.asiainfo.lop.net.VolleyInterface;
import com.asiainfo.lop.net.VolleyRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

@EFragment(R.layout.main_fragment_crowd)
public class MainFragmentCrowd extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {
    @ViewById(R.id.top_bar_tv)
    TextView topbarTv;
    @ViewById(R.id.btn_back_iv)
    ImageView btnBackIv;
    @ViewById(R.id.btn_back_tv)
    TextView btnBackTv;

    @ViewById(R.id.crowd_list_shedu_info)
    TextView sheduInfoTv;
    @ViewById(R.id.crowd_list_zhaoshi_info)
    TextView zhaoshiInfoTv;
    @ViewById(R.id.crowd_list_shekong_info)
    TextView shekongInfoTv;
    @ViewById(R.id.crowd_list_shangfang_info)
    TextView shangfangInfoTv;
    @ViewById(R.id.crowd_list_xingshi_info)
    TextView xingshiInfoTv;

    @Click(R.id.main_crowd_attent_item_shedu)
    void sheduItemClick() {
        Intent indent = new Intent(getActivity(), CrowdDetailListActivity_.class);
        indent.putExtra("type", "shedu");
        startActivity(indent);
    }

    @Click(R.id.main_crowd_attent_item_zhaoshi)
    void zhaoshiItemClick() {
        Intent indent = new Intent(getActivity(), CrowdDetailListActivity_.class);
        indent.putExtra("type", "zhaoshi");
        startActivity(indent);
    }

    @Click(R.id.main_crowd_attent_item_shekong)
    void shekongItemClick() {
        Intent indent = new Intent(getActivity(), CrowdDetailListActivity_.class);
        indent.putExtra("type", "shekong");
        startActivity(indent);
    }

    @Click(R.id.main_crowd_attent_item_shangfang)
    void shangfangItemClick() {
        Intent indent = new Intent(getActivity(), CrowdDetailListActivity_.class);
        indent.putExtra("type", "shangfang");
        startActivity(indent);
    }

    @Click(R.id.main_crowd_attent_item_xingshi)
    void xingshiItemClick() {
        Intent indent = new Intent(getActivity(), CrowdDetailListActivity_.class);
        indent.putExtra("type", "xingshi");
        startActivity(indent);
    }

    @AfterViews
    void setTopBarTv() {
        topbarTv.setText("关注人群");
        btnBackTv.setVisibility(View.GONE);
        btnBackIv.setVisibility(View.GONE);
    }

    @ViewById(R.id.main_fragment_crowd_reflash_layout)
    SwipeRefreshLayout swipeRefreshLayout;

    @AfterViews
    void init() {
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
        swipeRefreshLayout.setOnRefreshListener(this);
        refreshData();
    }

    @Override
    public void onRefresh() {
        refreshData();
    }

    void refreshData() {
        String url = getString(R.string.rest_url_rooturl) + getString(R.string.rest_url_crowd_grouptypelist);

        VolleyInterface volleyInterface = new VolleyInterface(getActivity()) {
            @Override
            public void success(JSONObject result) {
                swipeRefreshLayout.setRefreshing(false);

                flag = result.getString("flag");
                reason = result.getString("reason");

                if (!RestResultInfo.RESULT_SUCCESS_FLAG.equals(flag)) {
                    Toast.makeText(getActivity(), reason, Toast.LENGTH_LONG).show();
                    return;
                }

                loadData(result.getJSONArray("data"));
            }

            @Override
            public void error(VolleyError volleyError) {
                swipeRefreshLayout.setRefreshing(false);
            }
        };
        VolleyRequest.get(getActivity(), url, "MainFragmentCrowd", volleyInterface);
    }

    /**
     * 加载数据
     *
     * @param data
     */
    void loadData(JSONArray data) {
        for (int i = 0; i < data.size(); i++) {
            JSONObject d = data.getJSONObject(i);
            String groupTypeId = d.getString("groupTypeId");
            String groupTypeName = d.getString("groupTypeName");
            String grougNum = d.getString("grougNum");
            String groupCnt = d.getString("groupCnt");
            String newGroupCnt = d.getString("newGroupCnt");

            String info = "人群总数 @1 人，新增 @2 人";
            info = info.replace("@1", groupCnt);
            info = info.replace("@2", newGroupCnt);

            switch (groupTypeId) {
                case "1": {
                    sheduInfoTv.setText(info);
                    break;
                }
                case "2": {
                    zhaoshiInfoTv.setText(info);
                    break;
                }
                case "3": {
                    shekongInfoTv.setText(info);
                    break;
                }
                case "4": {
                    shangfangInfoTv.setText(info);
                    break;
                }
                case "5": {
                    xingshiInfoTv.setText(info);
                    break;
                }
            }
        }
    }
}
